package com.dsr.service.impl;

import cn.hutool.core.date.DateTime;
import com.dsr.bean.OrderBean;
import com.dsr.consts.PageResult;
import com.dsr.consts.Result;
import com.dsr.consts.e.BaseProperty;
import com.dsr.consts.e.UserProperty;
import com.dsr.dao.OrdersDao;
import com.dsr.dao.ShopDao;
import com.dsr.entity.Orders;
import com.dsr.entity.Shop;
import com.dsr.exception.FlowerException;
import com.dsr.service.OrdersService;
import com.dsr.util.BasicUtil;
import com.dsr.util.IdWorker;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * (Orders)表服务实现类
 *
 * @author dsr
 * @since 2020-09-11
 */
@Service("ordersService")
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersDao ordersDao;

    @Autowired
    private ShopDao shopDao;

    @Autowired
    private IdWorker idWorker;

    @Override
    public Result getEntity(String id) {
        Orders entity = ordersDao.selectByPrimaryKey(id);
        return Result.success(entity);
    }

    @Override
    public Result save(Orders orders) {
        orders.setId(String.valueOf(idWorker.nextId()));
        orders.setDel(0);
        orders.setState(0);
        orders.setCreateDate(DateTime.now());
        orders.setUpdateDate(DateTime.now());
        orders.setUserId(BasicUtil.getToken(UserProperty.USER_ID));
        ordersDao.insertSelective(orders);
        return Result.success("保存成功");
    }

    @Override
    public Result update(Orders orders) {
        orders.setUpdateDate(DateTime.now());
        ordersDao.updateByPrimaryKeySelective(orders);
        return Result.success("修改成功");

    }

    @Override
    public Result delete(String id) {
        if (StringUtils.isNotEmpty(id)) {
            List<String> ids = Arrays.asList(id.split(","));
            Example example = new Example(Orders.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andIn("id", ids);
            Orders orders = new Orders();
            ordersDao.updateByExampleSelective(orders,example);
            return Result.success("取消成功");
        }
        return Result.error("请选择要取消的行");
    }

    @Override
    public Result query(int page, int size, Orders orders) {
        if(BasicUtil.getToken(BaseProperty.ROLE).equals(UserProperty.USER))
        {
            String userId = BasicUtil.getToken(UserProperty.USER_ID);
            if(StringUtils.isEmpty(userId)){
                throw new FlowerException(HttpStatus.NETWORK_AUTHENTICATION_REQUIRED,"登录失效");
            }
            orders.setUserId(userId);

        }
        PageHelper.startPage(page, size);
        List<OrderBean> entityList = ordersDao.queryOrderBean(orders);
        PageInfo<OrderBean> pageInfo = new PageInfo<>(entityList);
        return Result.success(new PageResult<OrderBean>(pageInfo.getTotal(), pageInfo.getList()));
    }

    @Override
    public Result list() {
        return Result.success(ordersDao.selectAll());
    }

    @Override
    public Result oneClickShipping() {
        Orders orders = new Orders();
        orders.setState(1);
        Example example = new Example(Orders.class);
        example.createCriteria().andEqualTo("state",0);
        ordersDao.updateByExampleSelective(orders,example);
        return Result.success("一键发货成功");
    }

    @Override
    public Result save(List<Orders> ordersList) {
        String userId = BasicUtil.getToken(UserProperty.USER_ID);
        if(StringUtils.isEmpty(userId)){
            throw new FlowerException(HttpStatus.NETWORK_AUTHENTICATION_REQUIRED,"登录失效");
        }
        List<String> flowerIds = new ArrayList<>();
        ordersList.forEach(item->{
            item.setId(String.valueOf(idWorker.nextId()));
            item.setUserId(BasicUtil.getToken(UserProperty.USER_ID));
            item.setCreateDate(DateTime.now());
            item.setUpdateDate(DateTime.now());
            item.setState(0);
            item.setDel(1);
            flowerIds.add(item.getFlowerId());
        });
        Example example = new Example(Shop.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("flowerId",flowerIds).andEqualTo("userId",BasicUtil.getToken(UserProperty.USER_ID));
        shopDao.deleteByExample(example);
        ordersDao.insertOrdersList(ordersList);
        return Result.success("保存成功");
    }

    @Override
    public Result mouthSum() {
        return null;
    }

}